<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<%
    var tableName = tb.fields[0].TABLE_NAME;
    //所有的主键
    var primaryKey = tb.fields.filter(field=>field.COLUMN_KEY=="PRI").map(field=>field.COLUMN_NAME);
    //所有的非主键
    var insert_list = tb.fields.filter(field=>field.COLUMN_KEY!="PRI").map(field=>field.COLUMN_NAME);
    %>
<mapper namespace="<%-props.packageName%>.<%-props.mapperPackage%>.<%-tb.className%>Mapper" >
    <!-- 使用redis做缓存 -->
	<cache type="<%-props.cacheClass%>" />
    
    <%-include(extrucPath(path.join(dirname,"resultMap.ejs")),{tb:tb,className:tb.className})%>

    <%-include(extrucPath(path.join(dirname,"column_list.ejs")),{tb:tb,className:tb.className})%>
    <%if(!props.Simple){%>
    <!-- 查询单表所有记录-->
    <select id="findAll" resultMap="BaseResultMap" >
       SELECT 
       <include refid="Base_Column_List" />
       FROM <%-tableName%>
    </select>
    <!-- 分页查询-->
    <select id="findByPage" resultMap="BaseResultMap" >
        <bind name="pageNum" value="(num-1)*size"/>
       SELECT 
       <include refid="Base_Column_List" />
	   FROM <%-tableName%>
       LIMIT #{num},#{size}
    </select>
 	<!-- 根据类动态SQL查询-->
    <select id="findByPojo" resultMap="CasCadeResult" >
        SELECT 
       <include refid="Base_Column_List" />
	   FROM <%-tableName%>
	   <trim prefix="WHERE" prefixOverrides="and">
            <%tb.fields.forEach(field=>{%>
            <if test="<%-under2Camel(field.COLUMN_NAME)%>!=null">and <%-field.COLUMN_NAME%> = #{<%-under2Camel(field.COLUMN_NAME)%>}</if><%})%>
        </trim>
    </select>
    <!-- 根据类动态SQL更新-->
    <update id="updateByPojo">
        update employees
        <set>
        <%tb.fields.forEach(field=>{%>
            <if test="<%-under2Camel(field.COLUMN_NAME)%>!=null"> <%-field.COLUMN_NAME%> = #{<%-under2Camel(field.COLUMN_NAME)%>},</if><%})%>
        </set>
        where employee_id = #{employeeId}
    </update>
    <%/*这里插入对主键和唯一索引的及联方法的处理*/%>
    <%-include(extrucPath(path.join(dirname,"uni_mapper.ejs")),{tb:tb,className:tb.className})%>

    <!-- 新增逻辑-->
    <insert id="insert" parameterType="<%=props.packageName%>.<%-props.entityPackage%>.<%-tb.className%>" 
        useGeneratedKeys="true" keyProperty="<%-primaryKey&&primaryKey[0]?under2Camel(primaryKey[0]):''%>"><!--不支持联合主键-->
        INSERT INTO
        <%-tableName%>
        (<include refid="Base_Insert_List"/>)
        VALUES
        (<%-insert_list.map(fName=>"#{"+under2Camel(fName)+"}").join(",")%>)
               
        
    </insert>
    <!-- 插入列表-->
    <insert id="insertList">
        insert into <%-tableName%> 
        (<include refid="Base_Insert_List"/>)
        values
        <foreach collection="list" item="<%-under2Camel(tableName)%>" separator=",">
            (<%-insert_list.map(fName=>"#{"+under2Camel(tableName)+"."+under2Camel(fName)+"}").join(",")%>)
        </foreach>
    </insert>
    
   <%}%>
 	
</mapper>
